package com.car.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.car.account.dto.addressDto.AddressByUserIdDto;
import com.car.account.entity.Address;
import com.car.account.entity.User;
import com.car.account.exception.AccountException;
import com.car.account.mapper.AddressMapper;
import com.car.account.param.addressParam.InsertAddressParam;
import com.car.account.param.addressParam.UpdateAddressParam;
import com.car.account.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.car.account.service.UserService;
import com.car.account.util.ListUtil;
import com.car.account.util.validatorUtil.PhoneNumberValidator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Resource
    private AddressService addressService;

    @Resource
    private UserService userService;

    @Override
    public List<AddressByUserIdDto> selectByUserId(Integer userId) throws Exception {
        //判断是否存在用户
        User user = userService.getById(userId);
        if (user == null || user.getUserState() == 3) throw new AccountException(1000,"用户不存在");
        //判断状态
        if (user.getUserState() == 1) throw new AccountException(1002,"用户已被禁用");
        //查询地址集合
        List<Address> addresses = addressService.list(new QueryWrapper<Address>()
                .eq("user_id", userId)
                .ne("address_state", 1));
        return ListUtil.copyToList(addresses, AddressByUserIdDto.class);
    }

    @Override
    @Transactional
    public void insertByUserId(InsertAddressParam param) throws Exception {
        //判断是否存在用户
        User user = userService.getById(param.getUserId());
        if (user == null || user.getUserState() == 3) throw new AccountException(1000,"用户不存在");
        //判断状态
        if (user.getUserState() == 1) throw new AccountException(1002,"用户已被禁用");
        //判断手机号格式
        if (!PhoneNumberValidator.isValidPhoneNumber(param.getConsigneePhone()))
            throw new AccountException(3001,"手机号格式不正确");
        //查询地址集合
        List<Address> addresses = addressService.list(new QueryWrapper<Address>()
                .eq("user_id", param.getUserId())
                .ne("address_state", 1));
        if (addresses.size() >= 10) throw new AccountException(3002,"最多绑定10个地址");
        //新增地址信息
        Address address = new Address();
        address.setConsigneeName(param.getConsigneeName());
        address.setConsigneePhone(param.getConsigneePhone());
        address.setAddressInfo(param.getAddressInfo());
        address.setUserId(param.getUserId());
        address.setAddressState(0);
        save(address);
    }

    @Override
    @Transactional
    public void deleteById(Integer userId, Integer addressId) throws Exception {
        //判断是否存在用户
        User user = userService.getById(userId);
        if (user == null || user.getUserState() == 3) throw new AccountException(1000,"用户不存在");
        //判断状态
        if (user.getUserState() == 1) throw new AccountException(1002,"用户已被禁用");
        //判断是否存在地址
        Address address = addressService.getById(addressId);
        if (address == null || address.getAddressState() == 1) throw new AccountException(3001,"地址不存在");
        //判断地址是否属于当前用户
        if (address.getUserId() != userId) throw new AccountException(3002,"地址与当前用户信息不匹配");
        //删除地址
        address.setAddressState(1);
        updateById(address);
    }

    @Override
    @Transactional
    public void updateAddressById(UpdateAddressParam param) throws Exception {
        //判断是否存在用户
        User user = userService.getById(param.getUserId());
        if (user == null || user.getUserState() == 3) throw new AccountException(1000,"用户不存在");
        //判断状态
        if (user.getUserState() == 1) throw new AccountException(1002,"用户已被禁用");
        //判断是否存在地址
        Address address = addressService.getById(param.getAddressId());
        if (address == null || address.getAddressState() == 1) throw new AccountException(3001,"地址不存在");
        //判断地址是否属于当前用户
        if (address.getUserId() != param.getUserId()) throw new AccountException(3002,"地址与当前用户信息不匹配");
        //判断手机号格式
        if (!PhoneNumberValidator.isValidPhoneNumber(param.getConsigneePhone()))
            throw new AccountException(3001,"手机号格式不正确");
        //更新地址信息
        address.setConsigneeName(param.getConsigneeName());
        address.setConsigneePhone(param.getConsigneePhone());
        address.setAddressInfo(param.getAddressInfo());
        updateById(address);
    }

    @Override
    public AddressByUserIdDto selectByAddressId(Integer userId, Integer addressId) throws Exception {
        //判断是否存在用户
        User user = userService.getById(userId);
        if (user == null || user.getUserState() == 3) throw new AccountException(1000,"用户不存在");
        //判断状态
        if (user.getUserState() == 1) throw new AccountException(1002,"用户已被禁用");
        //判断是否存在地址
        Address address = addressService.getById(addressId);
        if (address == null || address.getAddressState() == 1) throw new AccountException(3001,"地址不存在");
        //判断地址是否属于当前用户
        if (address.getUserId() != userId) throw new AccountException(3002,"地址与当前用户信息不匹配");
        return BeanUtil.toBean(address, AddressByUserIdDto.class);
    }

}
